package com.douyang.config;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyang.pojo.VideoBaseInfo;
import com.douyang.service.VideoBaseInfoService;
import com.douyang.utils.ESUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class DataInit implements ApplicationRunner {
    private int pageSize=1000;
    @Autowired
    VideoBaseInfoService videoBaseInfoService;
    @Autowired
    ESUtils esUtils;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        long count = videoBaseInfoService.count();
        for(int page=1;page<=(count+pageSize-1)/pageSize;page++) {
            int finalPage = page;
            threadPoolExecutor.submit(()->{
                Page<VideoBaseInfo> pageInfo=new Page<>(finalPage,pageSize);
                List<VideoBaseInfo> videoBaseInfoList=videoBaseInfoService.list(pageInfo);
                try {
                    esUtils.insertVideoBaseList(videoBaseInfoList);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }
}
